package com.llho.admin.product;

import java.io.File;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.llho.admin.BaseAction;
import com.llho.core.bo.CatalogBo;
import com.llho.core.bo.ImageBo;
import com.llho.core.bo.ProductBo;
import com.llho.core.domain.Catalog;
import com.llho.core.domain.Image;
import com.llho.core.domain.Product;
import com.opensymphony.xwork2.ModelDriven;

public class NewAction extends BaseAction implements ModelDriven<Product> {
	
	private File image;
	private String imageFileName;
	private String imageContentType;
	private Long idCatalog;
	private Product product = new Product();
	static final String UPLOAD = "images/product/";
	
	private ProductBo productBo;
	// DI via Spring
	public void setProductBo(ProductBo productBo) {
		this.productBo = productBo;
	}
	private ImageBo imageBo;
	// DI via Spring
	public void setImageBo(ImageBo imageBo){
		this.imageBo = imageBo;
	}
	
	private CatalogBo catalogBo;
	// DI via Spring
	public void setCatalogBo(CatalogBo catalogBo){
		this.catalogBo = catalogBo;
	}
	
	public String execute(){
		System.out.println(image.getAbsolutePath());
		System.out.println(imageFileName);
		try{
			
			String dir = ServletActionContext.getServletContext().getRealPath(UPLOAD);
			
			File uploadDir = new File(dir);
			if(!uploadDir.exists()){
				uploadDir.mkdirs();
			}
			
			Long randomName = Calendar.getInstance().getTimeInMillis();
			
			String extension = "";
			extension = imageFileName.substring(imageFileName.lastIndexOf("."), imageFileName.length());
			String newName = randomName + extension;
			
			File fileToCreate = new File(uploadDir, newName);

			if(image != null){
				FileUtils.copyFile(image, fileToCreate);
				//product.setUrl(UPLOAD + newName);
				Image ha = new Image(UPLOAD + newName, null);
				product.addImage(ha);
			}
			
			Catalog catalog = catalogBo.getById(idCatalog);
			product.setCatalog(catalog);
			
			if(product.getName()!= null && product.getName().length() > 0){
				productBo.addNew(product);
			}
			
			return redirect("listingProduct.llh");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	
		return "success";
	}

	@Override
	public Product getModel() {
		product = new Product();
		return product;
	}
	
	public Long getIdCatalog() {
		return idCatalog;
	}

	public void setIdCatalog(Long idCatalog) {
		this.idCatalog = idCatalog;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
}
